import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
//导入持久化依赖包
import VuexPersistence from "vuex-persist";
//实例化持久化插件对象
const vuexLocal = new VuexPersistence({
  storage: window.localStorage, //存储介质,
  key: "vuex", //存储到本地存储
});
export default new Vuex.Store({
  state: {
    appurl: "https://api.it120.cc/small4",
    historyList: [], //模糊搜索的历史记录
    token: "",
    user: [],
  },
  mutations: {
    // 添加历史记录
    addhistory(state, item) {
      // 当历史记录到8条的时候  末尾删除一个
      if (state.historyList.length >= 8) {
        state.historyList.pop();
      }
      state.historyList.unshift(item);
    },
    // 删除历史记录
    removehistory(state) {
      state.historyList = [];
    },
    adduser(state, data) {
      state.user = data;
      state.token = data.token;
    },
    removeuser(state) {
      state.user = [];
      state.token = "";
    },
  },
  getters: {},
  actions: {},
  plugins: [vuexLocal.plugin],
});
